package main;

public class BasicCreature implements Creature {
    protected final String name;

    protected int combat;
    protected int speed;
    protected int spot;

    protected double health;
    protected int maxHealth;

    protected double energy;

    protected BasicCreature(CreatureTemplate template) {
        this.name = template.getName();
        this.combat = template.getCombat();
        this.speed = template.getSpeed();
        this.spot = template.getSpot();

        this.maxHealth = template.getMaxHealth();
        this.health = this.maxHealth;

        this.energy = 1.0;
    }

    @Override
    public String toString() {
        return this.name;
    }

    @Override
    public int getCombat() {
        return energyAdjust(this.combat);
    }

    @Override
    public int getSpeed() {
        return energyAdjust(this.speed);
    }
    
    @Override
    public int getSpot() {
        return energyAdjust(this.spot);
    }

    @Override
    public double getEnergy() {
        return this.energy;
    }

    public int getMaxHealth() {
        return maxHealth;
    }

    public double getHealth() {
        return health;
    }

    @Override
    public void updateHealth(double health) {
        this.health += health;

        this.updateEnergy(0.01 * health);

        if (this.health < 0) {
            this.health = 0;
        }

        if (this.health > maxHealth) {
            this.health = maxHealth;
        }
    }

    public void updateEnergy(double energy) {
        this.energy += energy;

        if (this.energy > 1.0) {
            this.energy = 1.0;
        } else if (this.energy < 0.0) {
            this.energy = 0.0;
        }
    }

    @Override
    public boolean isAlive() {
        return this.health > 0;
    }

    protected int energyAdjust(int value) {
        return Math.max(1, (int) Math.round(value * Math.pow(energy, 1.0/3)));
    }
}
